/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.desk;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;

import com.ruipengkj.bean.TExePos;
import com.ruipengkj.bean.TExePosCatalog;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.component.RPJButtonDesk;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.service.TExePosService;
import com.ruipengkj.view.base.BasePagePanel;
/**
 * 说明：
 * 餐桌面板
 */
public class DeskPanel extends BasePagePanel<TExePos> implements ComponentListener{

	private static final long serialVersionUID = 1L;
	
	private JPanel dishNamePanel;//菜品（名称）面板
	
	private PagingComponent pagingComponent;//翻页组建
	private PageView<TExePos> pageView;//翻页主键
	
	private TExePosService TExePosService = BuildSpringApplication.getInstance().getTExePosServiceImpl();
	private String CatalogId = null;
	
	private ButtonGroup bg = new ButtonGroup();
	private int num = 20;//每頁的數量
	
	public DeskPanel() {
		init();
		this.addComponentListener(this);
	}
	
	private void init() {
		FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 20, 0);
		dishNamePanel = new JPanel(flowLayout);
		dishNamePanel.setOpaque(false);
		
		pageView = new PageView<TExePos>();
		pageView.setMaxResult(num);
		pagingComponent = new PagingComponent();
		pagingComponent.setPageDelegale(this);
		pagingComponent.setOpaque(false);
		pagingComponent.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		this.setLayout(new BorderLayout());
		this.add(dishNamePanel, BorderLayout.CENTER);
		this.add(pagingComponent, BorderLayout.SOUTH);initFlowPanel();
	}
	
	private void initFlowPanel() {
		int count = dishNamePanel.getComponentCount();
		if (count > num) {
			for (int i = count - 1; i >= num; --i) {
				dishNamePanel.remove(i);
			}
		} else {
			for (int i = count; i < num; ++i) {
				RPJButtonDesk jb = new RPJButtonDesk(" ", this);
				bg.add(jb);
				dishNamePanel.add(jb);
				
			}
		}
		
		termQuery(1);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		for (int i = dishNamePanel.getComponentCount() - 1; i >= 0; --i) {
			if (e.getSource() == dishNamePanel.getComponent(i)) {
				AddDesk(((RPJButtonDesk)e.getSource()).getTExePos());
			}
		}
	}
	
	public void byTypeQueryDesk(String CatalogId) {
		this.CatalogId = CatalogId;
		termQuery(1);
	}
	
	//条件查询
	@Override
	protected void termQuery(final int currentPage) {
		new Thread() {
			public void run(){
				try {
					TExePos tExePos = new TExePos();
					tExePos.setTExePosCatalog(new TExePosCatalog(CatalogId));
					
					pageView.setMaxResult(num);
					pageView.setCurrentPage(currentPage);
					PageModel<TExePos> pageModel = TExePosService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tExePos);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					//清空餐桌
					for (int i = dishNamePanel.getComponentCount() - 1; i >= 0; --i) {
						RPJButtonDesk db = (RPJButtonDesk)dishNamePanel.getComponent(i);
						db.setEnabled(false);
						db.setTExePos(null);
						db.setText(" ");
					}
					if(pageModel!=null){
						List<TExePos> tExePosList = pageModel.getResultList();
						//添加餐桌
						if(tExePosList!=null){
							for (int i = tExePosList.size() - 1; i >= 0; --i) {
								RPJButtonDesk db = (RPJButtonDesk)dishNamePanel.getComponent(i);
								
								db.setEnabled(true);
								db.setTExePos(tExePosList.get(i));
								db.repaint();
							}
							pagingComponent.show();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	
	@Override
	protected PageView<TExePos> getPageView() {
		return pageView;
	}
	
	public void AddDesk(TExePos TExePos) {}

	@Override
	public void clear() {
		
	}

	public void setButtonGroup(AbstractButton b) {
		bg.add(b);
	}
	
	//610 5 --> 122
	//30+65*x
	@Override
	public void componentHidden(ComponentEvent arg0) {
//		System.out.println("组件变得不可见时调用。");
	}

	@Override
	public void componentMoved(ComponentEvent c) {
//		System.out.println("组件位置更改时调用。");
//		System.out.println(c.getComponent().getHeight()+ "   " +c.getComponent().getWidth());
//		initFlowPanel();
	}

	@Override
	public void componentResized(ComponentEvent c) {
//		System.out.println("组件大小更改时调用。");
//		System.out.println(c.getComponent().getHeight()+ "   " +c.getComponent().getWidth());
		num = (c.getComponent().getHeight()-30)/100 * (c.getComponent().getWidth()/133);
		if ((num > dishNamePanel.getComponentCount() || num < dishNamePanel.getComponentCount()) && num != 0) {
			initFlowPanel();
			this.updateUI();
		}
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
//		System.out.println("组件变得可见时调用。");
	}

}
